2012.11.29  ޸Ķ֡յĴ룬intptr֡ڴռ
IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VCI_CAN_OBJ)) * 50);

2012.09.26  1001      C# ʹòȫ룬ṹ ڴ
޸շ֡   
C#нն֡ݣʹòȫ룬Ҫ趨ʹòȫ

1޸Ľṹ嶨
//2.CANϢ֡͡
unsafe public struct VCI_CAN_OBJ  //ʹòȫ
{
    public uint ID;
    public uint TimeStamp;
    public byte TimeFlag;
    public byte SendType;
    public byte RemoteFlag;//ǷԶ֡
    public byte ExternFlag;//Ƿչ֡
    public byte DataLen;

    public fixed byte Data[8];

    public fixed byte Reserved[3];

}

2޸ĺӿڶ

        //[DllImport("controlcan.dll")]
        //static extern UInt32 VCI_Receive(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pReceive, UInt32 Len, Int32 WaitTime);
        [DllImport("controlcan.dll", CharSet = CharSet.Ansi)]
        static extern UInt32 VCI_Receive(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, IntPtr pReceive, UInt32 Len, Int32 WaitTime);

3޸Ľչ

            IntPtr[] ptArray = new IntPtr[1];
            ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VCI_CAN_OBJ)) * 50);
            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * 1);

            Marshal.Copy(ptArray, 0, pt, 1);


            res = VCI_Receive(m_devtype, m_devind, m_canind, pt, 50/*50*/, 100);
            ////////////////////////////////////////////////////////

            String str = "";
            for (UInt32 i = 0; i < res; i++)
            {
                VCI_CAN_OBJ obj = (VCI_CAN_OBJ)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(VCI_CAN_OBJ))), typeof(VCI_CAN_OBJ));
		
		}

            Marshal.FreeHGlobal(ptArray[0]);
            Marshal.FreeHGlobal(pt);

4ͿҪ޸